<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script>
      var win = nw.Window.get()
      win.resizeTo(1024, 768)
      win.moveTo(0, 0)
    </script>
    <style type="text/css">
      * {
        margin: 0;
        padding: 0;
      }
      html, body {
        width: 100%;
        height: 100%;
      }
      body {
        text-align: center;
        background: url("./bg.png") no-repeat;
        overflow: hidden;
        background-size: 100% 100%;
        font-weight: bold;
        color: #D40000;
      }
      #container {
        min-width: 1000px;
        min-height: 700px;
      }
      #title {
        font-size: 100px;
        margin-top: 80px;
      }
      #disc {
        font-size: 40px;
        margin: 10px 0;
      }
      #image {
        margin-top: 20px;
        max-height: 280px;
        border: 1px solid #E23540FF;
        border-radius: 20px;
      }
      #list {
        margin: 0 auto;
        max-width: 800px;
      }
      #list span {
        display: inline-block;
        width: 160px;
        font-size: 36px;
        margin-top: 8px;
      }
    </style>
  </head>

  <body>
    <div id="container">
      <div id="title">XX公司年会</div>
      <div id="disc">奖品描述</div>
      <img id="image" />
      <div id="list"></div>
    </div>
    <script>
      var fs = require('fs')
      var steps = null
      var step = 0
      var names = null
      var state = ''
      
      var disc = document.getElementById('disc')
      var image = document.getElementById('image')
      var list = document.getElementById('list')

      function reloadConf(func) {
        fs.readFile('names.ini', 'utf8', function(err, data) {
          names = data.split('\n').map(x => x.split(','))
        })
        fs.readFile('./steps.json', 'utf8', function(err, data) {
          steps = eval(data)
          if (func) func()
        })
      }

      function saveConf(func) {
        fs.writeFile('./steps.json', JSON.stringify(steps), function(err) {
          if (err) {
            alert(err)
          }
        })
        fs.writeFile('./names.ini', names.map(x => x.join(',')).join('\n'), function(err) {
          if (err) {
            alert(err)
          }
        })
      }

      function showPic(data) {
        disc.innerHTML = data.disc
        image.src = data.image
        image.style.display = 'inline'
        list.style.display = 'none'
        while (list.hasChildNodes()) {
          list.removeChild(list.firstChild)
        }
      }

      function showBlink(data) {
        disc.innerHTML = data.disc
        image.style.display = 'none'
        list.style.display = 'block'
        var spans = []
        for (var i = 0; i < data.count; ++i) {
          var span = document.createElement('span')
          list.appendChild(span)
          spans.push(span)
        }

        function doBlink() {
          if (state == 'showBlink') {
            names.sort(function() {
              return 0.5 - Math.random()
            })
            for (var i = 0; i < data.count; ++i) {
              spans[i].innerHTML = names[i][0]
            }
            window.requestAnimationFrame(doBlink)
          }
        }

        window.requestAnimationFrame(doBlink)
      }

      function showList(data) {
        disc.innerHTML = data.disc
        image.style.display = 'none'
        list.style.display = 'block'
        while (list.hasChildNodes()) {
          list.removeChild(list.firstChild)
        }
        for (var i = 0; i < data.list.length; ++i) {
          var span = document.createElement('span')
          span.innerHTML = data.list[i]
          list.appendChild(span)
        }
      }

      function nextStep() {
        var data = steps[step]
        if (state == 'showPic') {
          data.list = data.list || []
          if (data.list.length > 0) {
            state = 'showList'
            showList(data)
          } else {
            state = 'showBlink'
            showBlink(data)
          }
        } else if (state == 'showBlink') {
          if (data.list.length > 0) {
            state = 'showList'
            showList(data)
          }
        } else if (state == 'showList') {
          if (step < (steps.length - 1)) {
            ++step
            state = ''
            nextStep()
          }
        } else {
          state = 'showPic'
          showPic(data)
        }
      }

      function previousStep() {
        if (step > 0) {
          --step
        }
        state = ''
        nextStep()
      }

      function drawPrize() {
        if (state == 'showBlink') {
          var data = steps[step]
          names.sort(function (a, b) {
            if (a[1] <= data.level && b[1] > data.level) {
              return -1
            }
            return 0
          })
          var luck = names.splice(0, data.count)
          data.list = luck.map(x => x[0])
          saveConf()
          nextStep()
        }
      }

      document.addEventListener('keydown', function(e) {
        e=e||window.event
        if (e.keyCode == 56) {
          // 8
          reloadConf()
        } else if (e.keyCode == 52) {
          // 4
          nextStep()
        } else if (e.keyCode == 49) {
          // 1
          previousStep()
        } else if (e.keyCode == 32) {
          // 空格
          drawPrize()
        } else if (e.keyCode == 70) {
          // f
          win.toggleFullscreen()
        }
      })
      
      reloadConf(nextStep)
    </script> 
  </bdoy>
</html>